package org.leviatan.dataharbour.core.model.control;

import org.leviatan.common.keyutils.KeyGenerator;
import org.leviatan.dataharbour.core.model.DataSource;
import org.leviatan.dataharbour.core.model.HarbourRequest;
import org.leviatan.dataharbour.core.model.impl.DataSourceImpl;
import org.w3c.dom.Element;

public abstract class DataSourceHandler {
	
	public static void persistRequest(DataSource ds, HarbourRequest req) {
		
		String dsId = ds.getId();
		
		/*
		Vector persistenceResources = 
			Register.getInstance().getDataSource2persistenceResources().get(dsId);
		
		for (Iterator i = persistenceResources.iterator(); i.hasNext(); ) {
			
			PersistenceResource perRes = (PersistenceResource) i.next();
			
			perRes.persist(req);
			
		}
		*/
	}
	
	public static void handleRequest(DataSource ds, HarbourRequest req) {
		/*
		Vector persistenceResources =
			RegisterHandler.getDataSourcesPersistenceResourceKeys(ds.getId());
		
		for (Iterator i = persistenceResources.iterator(); i.hasNext(); ) {
			
			PersistenceResource perRes = (PersistenceResource) i.next();
			
			perRes.persist(req);	
		}
		*/	
	}
	
	public static void setNewUniqueIdForDataSource(DataSource ds) {
		ds.setId(KeyGenerator.generateUniqueKeyAsString());
	}
	
	public static DataSource createNewDataSource(String name) {
		DataSource ds = new DataSourceImpl();
		DataSourceHandler.setNewUniqueIdForDataSource(ds);
		ds.setName(name);
		return ds;
	}
	
	
	public static DataSource instantiateModel(Element e) {
		String id = e.getElementsByTagName("id").item(0).getTextContent();
		String name = e.getElementsByTagName("name").item(0).getTextContent();
		
		DataSource ds = new DataSourceImpl();
		ds.setId(id);
		ds.setName(name);
		
		return ds;
	}
	
	public static String convertToXml(DataSource ds) {
		String xml = "";
		xml += "\n\t\t<id><![CDATA["+ ds.getId()+ "]]></id>";
		xml += "\n\t\t<name><![CDATA["+ ds.getName()+ "]]></name>";
		return xml;
	}
	
}
